package wx;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class PayMain {


    public static final String REQUEST_URL = "http://119.28.34.43/mctrpc/order/mkReceiptOrder.htm";

    public static final String QUERY_URL = "";

    public static final String MERCHANT_NO = "90000022";

    public static final String KEY = "aqww960nwg6gcs7h9m0woz65hrgzj5ph64knk3mitxp0q3bd6vn0u5afetnnxkjyo0ziwkcz";


    /**
     * 微信扫码支付参数
     *
     * @return
     */
    public static String weixinScanPay() {
        HashMap<String, String> params = new HashMap<>();
        params.put("version", "1.1");
        params.put("merchantId", MERCHANT_NO);
        params.put("merchantTime", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        params.put("traceNO", System.currentTimeMillis() + "");
        params.put("requestAmount", "3.00"); // 3元
        params.put("returnUrl", "http://www.baidu.com");
        params.put("notifyUrl", "http://uupp777.vicp.cc:51128/notify/shyqNotify");
        params.put("goodsName", "商品");
        params.put("goodsCount", "1");
        params.put("ip", "192.168.0.1");
        params.put("paymentCount", "1");
        params.put("payment_1", "3_3.00"); //支付类型_支付金额
        System.out.println("开始签名》》》》》》》》》》》》》》》》》》》》");
        String sign = PayUtils.sign(params, KEY);
        params.put("sign", sign);
        System.out.println("发送请求》》》》》》》》》》》》》》》》》》》》");
        String response = PayUtils.makeRequest(params, REQUEST_URL);
        System.out.println(response);
        System.out.println("验证签名》》》》》》》》》》》》》》》》》》》》");
        boolean b = PayUtils.checkSign(response, KEY);
        System.out.println("签名结果:result:" + (b ? "OK" : "FAIL"));
        int begin = response.indexOf("{\"");
        int end = response.indexOf("\"}|");
        response = response.substring(begin,end)+"\"}";
        System.out.println(response);
        JSONObject jb = JSON.parseObject(response);
        System.out.println(jb);
        System.out.println(jb.getString("payments"));
        JSONArray ja = jb.getJSONArray("payments");
        System.out.println(ja.getJSONObject(0).getJSONObject("itemResponseMsg").getString("barcodeInfo"));
        //解析出 localUrl 参数，从网页或者app里面唤起微信
        return response;
    }

    /**
     * 微信h5支付
     *
     * @return
     */
    public static String weixinH5Pay() {
        HashMap<String, String> params = new HashMap<>();
        params.put("version", "1.1");
        params.put("merchantId", MERCHANT_NO);
        params.put("merchantTime", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        params.put("traceNO", System.currentTimeMillis() + "");
        params.put("requestAmount", "3"); // 3元 单位: 元
        params.put("returnUrl", "http://www.baidu.com");
        params.put("notifyUrl", "http://www.baidu.com/notify");
        params.put("goodsName", "商品");
        params.put("goodsCount", "1");
        params.put("ip", "192.168.0.1");
        params.put("paymentCount", "1");
        params.put("payment_1", "5_3"); //支付类型_支付金额
        params.put("extend", "app_name=xxx&package_name=com.test.xxx&test"); //根据自己的情况填写
        System.out.println("开始签名》》》》》》》》》》》》》》》》》》》》");
        String sign = PayUtils.sign(params, KEY);
        params.put("sign", sign);
        System.out.println("发送请求》》》》》》》》》》》》》》》》》》》》");
        String response = PayUtils.makeRequest(params, REQUEST_URL);
        System.out.println(response);
        System.out.println("验证签名》》》》》》》》》》》》》》》》》》》》");
        boolean b = PayUtils.checkSign(response, KEY);
        System.out.println("签名结果:result:" + (b ? "OK" : "FAIL"));

        //解析出 localUrl 参数，从网页或者app里面唤起微信'
        return response;
    }


    public static void main(String[] args) {
        weixinScanPay();

    }


}
